package com.wshare.service.impl;

import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.wshare.service.interfaces.IMailService;
@Service
public class MailServiceImpl implements IMailService{

	@Autowired
	private MailSender mailSender;

	@Override
	public void sendMail(String to, String subject, String body) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(to);
		message.setFrom("danh.mai@axonactive.vn");
		message.setSubject(subject);
		message.setText(body);
		try {
			mailSender.send(message);
		} catch (Exception e) {
			System.out.println("not be send: " + e);
		}
	}

	@Override
	public void sendListMail(List<String> lmail, String subject, String body) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(lmail.toArray(new String[lmail.size()]));
		message.setFrom("danh.mai@axonactive.vn");
		message.setSubject(subject);
		message.setText(body);
		try {
			mailSender.send(message);
		} catch (Exception e) {
			System.out.println("not be send: " + e);
		}
	}

	@Override
	public void sendMailHTML(String to, String subject, String body) {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			sender.setHost("exchange.aavn.local");
			MimeMessage message = sender.createMimeMessage();
			// use the true flag to indicate you need a multipart message
			MimeMessageHelper helper;
			helper = new MimeMessageHelper(message, true,"UTF-8");
			helper.setSubject(subject);
			helper.setTo(to);
			helper.setFrom("danh.mai@axonactive.vn");
			// use the true flag to indicate the text included is HTML
			helper.setText(body, true);
			sender.send(message);
		} catch (MessagingException e) {
			System.out.println("Error send html mail: " + e);
		}

	}

	@Override
	public void sendListMailHTML(List<String> lmail, String subject, String body) {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			sender.setHost("exchange.aavn.local");
			MimeMessage message = sender.createMimeMessage();
			// use the true flag to indicate you need a multipart message
			MimeMessageHelper helper;
			helper = new MimeMessageHelper(message, true,"UTF-8");
			helper.setSubject(subject);
			helper.setTo(lmail.toArray(new String[lmail.size()]));
			helper.setFrom("danh.mai@axonactive.vn");
			// use the true flag to indicate the text included is HTML
			helper.setText(body, true);
			sender.send(message);
		} catch (MessagingException e) {
			System.out.println("Error send html mail: " + e);
		}

	}

	@Override
	public void sendCcListMailHTML(String[] lto, String[] lcc, String subject,
			String body) {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			sender.setHost("exchange.aavn.local");
			MimeMessage message = sender.createMimeMessage();
			// use the true flag to indicate you need a multipart message
			MimeMessageHelper helper;
			helper = new MimeMessageHelper(message, true,"UTF-8");
			helper.setSubject(subject);
			helper.setTo(lto);
			helper.setCc(lcc);
			helper.setFrom("danh.mai@axonactive.vn");
			// use the true flag to indicate the text included is HTML
			helper.setText(body, true);
			sender.send(message);
		} catch (MessagingException e) {
			System.out.println("Error send html mail: " + e);
		}
		
	}

}
